package com.cbank.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cbank.constants.Page;
import com.cbank.constants.Result;
import com.cbank.constants.ResultCode;
import com.cbank.domain.ModuleShow;
import com.cbank.dto.ModuleShowDto;
import com.cbank.service.ModuleShowDetailService;
import com.cbank.service.ModuleShowService;
import com.cbank.utils.DateTimeUtil;
import com.cbank.utils.JSONUtil;
import com.cbank.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/news")
public class NewsController {
    @Autowired
    private ModuleShowService moduleShowService;
    @Autowired
    private ModuleShowDetailService moduleShowDetailService;

    /**
     * 新增新闻
     * @param moduleShowText
     * @param moduleId
     */
    @RequestMapping(value = "/savenews",method = RequestMethod.POST)
    @ResponseBody
    public Result<String> saveNews(@RequestParam(value = "moduleShowText",required = false,defaultValue = "")String moduleShowText, @RequestParam(value = "moduleId",required = true)Integer moduleId){
        JSONArray moduleShowArray= JSONUtil.parseArray(moduleShowText);
        for (int i=0;i<moduleShowArray.size();i++){
            JSONObject moduleShow=JSONUtil.parseObject(moduleShowArray.get(i).toString());
            Integer newsType=moduleShow.getInteger("newsType");
            String title=moduleShow.getString("title");
            String source=moduleShow.getString("source");
            String pictureUrl=moduleShow.getString("pictureUrl");
            String sourceUrl=moduleShow.getString("sourceUrl");
            String publishTime=moduleShow.getString("publishTime");
            ModuleShow moduleShowDb=new ModuleShow();
            moduleShowDb.setNewsType(newsType);
            moduleShowDb.setTitle(title);
            moduleShowDb.setSource(source);
            moduleShowDb.setPictureUrl(pictureUrl);
            moduleShowDb.setSourceUrl(sourceUrl);
            moduleShowDb.setPublishTime(publishTime);
            moduleShowDb.setModuleId(moduleId);
            this.moduleShowService.save(moduleShowDb);
        }
        return new Result<String>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(), StringUtil.EMPTY_STRING);
    }


    /**
     * 修改单个新闻的详细信息
     * @param moduleShowDto
     * @return
     */
    @RequestMapping(value = "/saveone",method = RequestMethod.POST)
    @ResponseBody
    public Result<String> saveNewsById(ModuleShowDto moduleShowDto){
        ModuleShow moduleShow=this.moduleShowService.getById(moduleShowDto.getId());
        moduleShow.setPictureUrl(moduleShowDto.getPictureUrl());
        moduleShow.setTitle(moduleShowDto.getTitle());
        moduleShow.setNewsType(moduleShowDto.getNewsType());
        moduleShow.setSourceUrl(moduleShowDto.getSourceUrl());
        moduleShow.setSource(moduleShowDto.getSource());
        moduleShow.setPublishTime(moduleShowDto.getPublishTime());
        this.moduleShowService.save(moduleShow);
        return new Result<String>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),StringUtil.EMPTY_STRING);
    }

    /**
     * 展示单个newsModuleShow的详细信息
     * @param moduleShowId
     * @return
     */
    @RequestMapping(value = "/one",method = RequestMethod.GET)
    @ResponseBody
    public Result<ModuleShow> newsModuleShow(@RequestParam(value = "moduleShowId",required = true)Integer moduleShowId){
        ModuleShow moduleShow=this.moduleShowService.getById(moduleShowId);
        return new Result<ModuleShow>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),moduleShow);
    }

    /**
     * 展示多条moduleShow的信息
     * @param moduleId
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    @ResponseBody
    public Result<Page> newsModuleShowAll(@RequestParam(value = "modueId",required = true)Integer moduleId, @RequestParam(value = "pageIndex",required = false,defaultValue = "1")Integer pageIndex,
                                          @RequestParam(value = "pageSize",required = false,defaultValue = "20")Integer pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        List<ModuleShow> newsModuleShowAll=this.moduleShowService.getListByModuleId(moduleId);
        PageInfo<ModuleShow> pageInfo=new PageInfo<>(newsModuleShowAll);
        Page<ModuleShow> page=new Page<>();
        page.setContent(newsModuleShowAll);
        page.setPageIndex(pageIndex);
        page.setPageSize(pageSize);
        page.setTotal((int) pageInfo.getTotal());
        page.setTotalPage();
        return new Result<Page>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),page);
    }
}
